package ntut.andengine.game;

import java.util.ArrayList;
import java.util.List;

import ntut.andengine.physics.ContactEvent;
import ntut.andengine.physics.PhysicalComponent;
import ntut.andengine.physics.DeathLight.Direction;
import ntut.andengine.physics.PhysicalComponent.ComponentColor;
import ntut.andengine.physics.PhysicalFactory;

import org.anddev.andengine.engine.handler.IUpdateHandler;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import android.widget.Toast;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;

public class GameLevel9 extends GameLevel {

	GameLevel mLevel;
	List<PhysicalComponent> mBoxList;
	
	@Override
	public void onLoadResources() {
		super.onLoadResources();
		mLevel = this;
		mBoxList = new ArrayList<PhysicalComponent>();
		
		BitmapTexture backgroundBitmapTexture = new BitmapTexture(512, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion backgroundTextureRegion = BitmapTextureRegionFactory.createFromAsset(backgroundBitmapTexture, this, "universe.png", 0, 0);	
		mEngine.getTextureManager().loadTexture(backgroundBitmapTexture);	
		Sprite background = new Sprite(0, 0, backgroundTextureRegion);
		mScene.attachChild(background);
	}
	
	@Override
	public Scene onLoadScene() {
		
        PhysicalComponent gate = PhysicalFactory.CreateTransmissionGate(this, new Vector2(400, 60));//240, 200
        gate.getBody().setUserData(new ContactEvent("Gate"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
				
				// LEVEL COMPLETE!!
				if(body.getUserData().toString() == "Player"){
					mLevel.runOnUiThread(new Runnable(){

						@Override
						public void run() {
							mMusic.pause();
							successSound.play();
							Toast.makeText(mLevel, "Level Completed!", Toast.LENGTH_SHORT).show();	
						}
			        	
			        });
					
					startActivity(GameLevel9.this, GameLevel20.class);
				}
			
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {
				// TODO Auto-generated method stub
				
			}
		
		});
        
        PhysicalFactory.CreateWall(this, new Vector2(20, 400), 40, 800, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(460, 400), 40, 800, ComponentColor.White);
        PhysicalFactory.CreateWall(this, new Vector2(370, 140), 140, 40, ComponentColor.White);
        mBoxList.add(PhysicalFactory.CreateBox(this, new Vector2(80, 200), ComponentColor.Blue));
        mBoxList.add(PhysicalFactory.CreateBox(this, new Vector2(160, 200), ComponentColor.Blue));//320, 280
        mBoxList.add(PhysicalFactory.CreateBox(this, new Vector2(240, 200), ComponentColor.Blue));
        PhysicalFactory.CreateBox(this, new Vector2(400, 200), ComponentColor.Blue);//80, 200
        mBoxList.add(PhysicalFactory.CreateBox(this, new Vector2(320, 200), ComponentColor.Blue));

        for (int i = 0; i < 5; i++)
        {
        	mBoxList.add(PhysicalFactory.CreateBox(this, new Vector2(80+80*i, 360), ComponentColor.Blue));
        }
        for (int i = 0; i < 5; i++)
        {
        	mBoxList.add(PhysicalFactory.CreateBox(this, new Vector2(80 + 80 * i, 520), ComponentColor.Blue));
        }
        PhysicalFactory.CreateDeathLight(this, new Vector2(220, 100), 40, 40, Direction.Right);
        PhysicalFactory.CreateLaunchingPad(this, new Vector2(160, 100), true);//160, 80
        
        mSparkle = PhysicalFactory.CreateSparkle(this, new Vector2(240, 680), ComponentColor.Blue);
	
		this.mScene.registerUpdateHandler(this.mPhysicsWorld);
		mScene.registerUpdateHandler(new IUpdateHandler(){

			@Override
			public void onUpdate(float pSecondsElapsed) {

				setSameColor();
			}

			@Override
			public void reset() {
				// TODO Auto-generated method stub
				
			}
			
		});

		return mScene;
	}
	
	private void setSameColor(){
		for(PhysicalComponent box : mBoxList){
			if(box.getColor() != mSparkle.getColor())
				box.setColor(mSparkle.getColor());
		}
	}
}
